<!DOCTYPE html>
<html>
<head>
    <title>WebSocket连接测试</title>
    <meta charset="UTF-8">
</head>
<body>
    <h1>WebSocket连接测试</h1>
    <div id="status">正在连接...</div>
    <div id="messages"></div>
    <input type="text" id="messageInput" placeholder="输入测试消息" />
    <button onclick="sendTest()">发送测试</button>

    <script>
        const ws = new WebSocket('ws://115.190.136.40:9002');
        const statusDiv = document.getElementById('status');
        const messagesDiv = document.getElementById('messages');

        ws.onopen = function() {
            statusDiv.innerHTML = '<span style="color: green;">✅ WebSocket连接成功！</span>';
            console.log('WebSocket连接已建立');
        };

        ws.onmessage = function(event) {
            console.log('收到消息:', event.data);
            const messageDiv = document.createElement('div');
            messageDiv.innerHTML = '<strong>收到:</strong> ' + event.data;
            messagesDiv.appendChild(messageDiv);
        };

        ws.onclose = function() {
            statusDiv.innerHTML = '<span style="color: red;">❌ WebSocket连接已关闭</span>';
        };

        ws.onerror = function(error) {
            statusDiv.innerHTML = '<span style="color: red;">❌ WebSocket连接错误</span>';
            console.error('WebSocket错误:', error);
        };

        function sendTest() {
            if (ws.readyState === WebSocket.OPEN) {
                const message = {
                    "action": "get_characters"
                };
                ws.send(JSON.stringify(message));
                console.log('发送测试消息');
            } else {
                alert('WebSocket未连接');
            }
        }
    </script>
</body>
</html>
